package win.timloong.android.carwiki.activity;

import android.app.Activity;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import win.timloong.android.carwiki.R;
import win.timloong.android.carwiki.fragment.ChooseCarFragment;
import win.timloong.android.carwiki.fragment.MineFragment;
import win.timloong.android.carwiki.fragment.QuanziFragment;
import win.timloong.android.carwiki.fragment.IndexFragment;
import win.timloong.android.carwiki.util.ImageUtil;

public class MainActivity extends FragmentActivity implements View.OnClickListener {
    /**
     * 容器Id
     */
    private int containerId;
    private FragmentManager manager;
    /**
     * Tab包含的fragment
     */
    private Fragment[] fragments;
    /**
     * 当前所在Tab
     */
    private int index;

    private ImageView[] imageViews;
    private TextView[] textViews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initFragment();
    }

    /**
     * 初始化布局及事件
     */
    private void initView() {
        containerId = R.id.llContainer;

        findViewById(R.id.main_tab1).setOnClickListener(this);
        findViewById(R.id.main_tab2).setOnClickListener(this);
        findViewById(R.id.main_tab3).setOnClickListener(this);
        findViewById(R.id.main_tab4).setOnClickListener(this);

        imageViews = new ImageView[]{
                (ImageView) findViewById(R.id.main_tab1_img),
                (ImageView) findViewById(R.id.main_tab2_img),
                (ImageView) findViewById(R.id.main_tab3_img),
                (ImageView) findViewById(R.id.main_tab4_img)
        };

        textViews = new TextView[]{
                (TextView) findViewById(R.id.main_tab1_title),
                (TextView) findViewById(R.id.main_tab2_title),
                (TextView) findViewById(R.id.main_tab3_title),
                (TextView) findViewById(R.id.main_tab4_title),
        };
        ImageUtil.loadImage(R.mipmap.img_focus,imageViews[0]);
        textViews[0].setTextColor(Color.rgb(0,0,0));
    }

    /**
     * 初始化底部导航
     */
    private void initFragment() {
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        fragments = new Fragment[]{new IndexFragment(), new ChooseCarFragment(), new QuanziFragment(), new MineFragment()};

        for (int i = 0; i < fragments.length; i++) {
            transaction.add(containerId, fragments[i]);
            transaction.hide(fragments[i]);
        }
        transaction.show(fragments[0]);
        transaction.commit();
    }

    /**
     * 设置Fragment显示,隐藏
     *
     * @param fragmentNum
     */
    private void changeTab(int fragmentNum) {


        FragmentTransaction transaction = manager.beginTransaction();


        transaction.hide(fragments[index]);

        transaction.show(fragments[fragmentNum]);
        transaction.commit();

        //恢复成默认的
        ImageUtil.loadImage(R.mipmap.ic_launcher,imageViews[index]);

        textViews[index].setTextColor(Color.rgb(0,0,0));
        //设置选中的
        ImageUtil.loadImage(R.mipmap.img_focus,imageViews[fragmentNum]);

        textViews[fragmentNum].setTextColor(Color.rgb(255,0,0));

        index = fragmentNum;


    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_tab1:
                changeTab(0);
                break;
            case R.id.main_tab2:
                changeTab(1);
                break;
            case R.id.main_tab3:
                changeTab(2);
                break;
            case R.id.main_tab4:
                changeTab(3);
                break;
        }
    }

}
